<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          clearable
          placeholder="请输入产品名称"
          @keyup.enter.native="handleQuery"
        />
        this.form = response.data;
      </el-form-item>
      <el-form-item label="产品分属" prop="productDivision">
        <el-input
          v-model="queryParams.productDivision"
          clearable
          placeholder="请输入产品分属"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产地" prop="productArea">
        <el-input
          v-model="queryParams.productArea"
          clearable
          placeholder="请输入产地"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="已有认证" prop="productCertificate">
        <el-input
          v-model="queryParams.productCertificate"
          clearable
          placeholder="请输入已有认证"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="驱动器数量" prop="productActuator">
        <el-input
          v-model="queryParams.productActuator"
          clearable
          placeholder="请输入驱动器数量"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="防护等级" prop="productLevel">
        <el-input
          v-model="queryParams.productLevel"
          clearable
          placeholder="请输入防护等级"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="颜色" prop="productColour">
        <el-input
          v-model="queryParams.productColour"
          clearable
          placeholder="请输入颜色"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称英文" prop="productNameE">
        <el-input
          v-model="queryParams.productNameE"
          clearable
          placeholder="请输入产品名称英文"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品型号英文" prop="productModelE">
        <el-input
          v-model="queryParams.productModelE"
          clearable
          placeholder="请输入产品型号英文"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品分属英文" prop="productDivisionE">
        <el-input
          v-model="queryParams.productDivisionE"
          clearable
          placeholder="请输入产品分属英文"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品分属英文" prop="productAreaE">
        <el-input
          v-model="queryParams.productAreaE"
          clearable
          placeholder="请输入产品分属英文"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="防护等级英文" prop="productLevelE">
        <el-input
          v-model="queryParams.productLevelE"
          clearable
          placeholder="请输入防护等级英文"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="已有认知英文" prop="productCertificateE">
        <el-input
          v-model="queryParams.productCertificateE"
          clearable
          placeholder="请输入已有认知英文"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="驱动器数量英文" prop="productActuatorE">
        <el-input
          v-model="queryParams.productActuatorE"
          clearable
          placeholder="请输入驱动器数量英文"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="颜色英文" prop="productColourE">
        <el-input
          v-model="queryParams.productColourE"
          clearable
          placeholder="请输入颜色英文"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">{{$t("search")}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t("reset")}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:products:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >{{$t("add")}}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:products:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="mini"
          type="success"
          @click="handleUpdate"
        >{{$t("edit")}}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:products:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          @click="handleDelete"
        >{{$t("delete")}}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:products:export']"
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="handleExport"
        >{{$t("export")}}
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productsList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="产品ID" prop="productId"/>
      <el-table-column align="center" label="产品名称" prop="productName"/>
      <el-table-column align="center" label="产品型号" prop="productModel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.productModel"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="产品分属" prop="productDivision"/>
      <el-table-column align="center" label="产地" prop="productArea"/>
      <el-table-column align="center" label="已有认证" prop="productCertificate"/>
      <el-table-column align="center" label="驱动器数量" prop="productActuator"/>
      <el-table-column align="center" label="防护等级" prop="productLevel"/>
      <el-table-column align="center" label="颜色" prop="productColour"/>
      <el-table-column align="center" :label="$t('remark')" prop="remark"/>
      <el-table-column align="center" label="产品名称英文" prop="productNameE"/>
      <el-table-column align="center" label="产品型号英文" prop="productModelE"/>
      <el-table-column align="center" label="产品分属英文" prop="productDivisionE"/>
      <el-table-column align="center" label="产品分属英文" prop="productAreaE"/>
      <el-table-column align="center" label="防护等级英文" prop="productLevelE"/>
      <el-table-column align="center" label="已有认知英文" prop="productCertificateE"/>
      <el-table-column align="center" label="驱动器数量英文" prop="productActuatorE"/>
      <el-table-column align="center" label="颜色英文" prop="productColourE"/>
      <el-table-column align="center" class-name="small-padding fixed-width" :label="$t('controls')">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:products:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >{{$t("edit")}}
          </el-button>
          <el-button
            v-hasPermi="['system:products:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >{{$t("delete")}}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改角色信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称"/>
        </el-form-item>
        <el-form-item label="产品型号" prop="productModel">
          <el-input v-model="form.productModel" placeholder="请输入内容" type="textarea"/>
        </el-form-item>
        <el-form-item label="产品分属" prop="productDivision">
          <el-input v-model="form.productDivision" placeholder="请输入产品分属"/>
        </el-form-item>
        <el-form-item label="产地" prop="productArea">
          <el-input v-model="form.productArea" placeholder="请输入产地"/>
        </el-form-item>
        <el-form-item label="已有认证" prop="productCertificate">
          <el-input v-model="form.productCertificate" placeholder="请输入已有认证"/>
        </el-form-item>
        <el-form-item label="驱动器数量" prop="productActuator">
          <el-input v-model="form.productActuator" placeholder="请输入驱动器数量"/>
        </el-form-item>
        <el-form-item label="防护等级" prop="productLevel">
          <el-input v-model="form.productLevel" placeholder="请输入防护等级"/>
        </el-form-item>
        <el-form-item label="颜色" prop="productColour">
          <el-input v-model="form.productColour" placeholder="请输入颜色"/>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志"/>
        </el-form-item>
        <el-form-item :label="$t('remark')" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入内容" type="textarea"/>
        </el-form-item>
        <el-form-item label="产品名称英文" prop="productNameE">
          <el-input v-model="form.productNameE" placeholder="请输入产品名称英文"/>
        </el-form-item>
        <el-form-item label="产品型号英文" prop="productModelE">
          <el-input v-model="form.productModelE" placeholder="请输入产品型号英文"/>
        </el-form-item>
        <el-form-item label="产品分属英文" prop="productDivisionE">
          <el-input v-model="form.productDivisionE" placeholder="请输入产品分属英文"/>
        </el-form-item>
        <el-form-item label="产品分属英文" prop="productAreaE">
          <el-input v-model="form.productAreaE" placeholder="请输入产品分属英文"/>
        </el-form-item>
        <el-form-item label="防护等级英文" prop="productLevelE">
          <el-input v-model="form.productLevelE" placeholder="请输入防护等级英文"/>
        </el-form-item>
        <el-form-item label="已有认知英文" prop="productCertificateE">
          <el-input v-model="form.productCertificateE" placeholder="请输入已有认知英文"/>
        </el-form-item>
        <el-form-item label="驱动器数量英文" prop="productActuatorE">
          <el-input v-model="form.productActuatorE" placeholder="请输入驱动器数量英文"/>
        </el-form-item>
        <el-form-item label="颜色英文" prop="productColourE">
          <el-input v-model="form.productColourE" placeholder="请输入颜色英文"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{$t("ok")}}</el-button>
        <el-button @click="cancel">{{$t("cancel")}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addProducts, delProducts, getProducts, listProducts, updateProducts} from "@/api/system/products";

export default {
  name: "Products",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色信息表格数据
      productsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        productModel: null,
        productDivision: null,
        productArea: null,
        productCertificate: null,
        productActuator: null,
        productLevel: null,
        productColour: null,
        productNameE: null,
        productModelE: null,
        productDivisionE: null,
        productAreaE: null,
        productLevelE: null,
        productCertificateE: null,
        productActuatorE: null,
        productColourE: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productName: [
          {required: true, message: "产品名称不能为空", trigger: "blur"}
        ],
        productModel: [
          {required: true, message: "产品型号不能为空", trigger: "blur"}
        ],
        productDivision: [
          {required: true, message: "产品分属不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询角色信息列表 */
    getList() {
      this.loading = true;
      listProducts(this.queryParams).then(response => {
        this.productsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productId: null,
        productName: null,
        productModel: null,
        productDivision: null,
        productArea: null,
        productCertificate: null,
        productActuator: null,
        productLevel: null,
        productColour: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        productNameE: null,
        productModelE: null,
        productDivisionE: null,
        productAreaE: null,
        productLevelE: null,
        productCertificateE: null,
        productActuatorE: null,
        productColourE: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.productId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加角色信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const productId = row.productId || this.ids
      getProducts(productId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改角色信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.productId != null) {
            updateProducts(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('modifiedSuccessfully'));
              this.open = false;
              this.getList();
            });
          } else {
            addProducts(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('newSuccess'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productIds = row.productId || this.ids;
      this.$modal.confirm('是否确认删除角色信息编号为"' + productIds + this.$t('dataItem')).then(function () {
        return delProducts(productIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('successfullyDeleted'));
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/products/export', {
        ...this.queryParams
      }, `products_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
